/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */
package cn.afterturn.easypoi.word.parse;

import cn.afterturn.easypoi.cache.WordCache;
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import cn.afterturn.easypoi.word.entity.MyXWPFDocument;
import cn.afterturn.easypoi.word.entity.bookmark.*;
import cn.afterturn.easypoi.word.entity.bookmark.ExportExecel.Constant;
import cn.afterturn.easypoi.word.entity.bookmark.ExportExecel.TablePara;
import cn.afterturn.easypoi.word.parse.Algorithm.ParseParagraphAlgo;
import cn.afterturn.easypoi.word.parse.excel.ExcelMapParse;
import com.google.common.collect.Maps;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.*;

import static cn.afterturn.easypoi.util.PoiElUtil.*;

/**
 * 解析07版的Word,替换文字,生成表格,生成图片
 *
 * @author JueYue
 * 2013-11-16
 * @version 1.0
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class ParseWord07BookMark {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParseWord07BookMark.class);

    public ParseParagraphAlgo getParagraphAlgo() {
        return paragraphAlgo;
    }

    public void setParagraphAlgo(ParseParagraphAlgo paragraphAlgo) {
        this.paragraphAlgo = paragraphAlgo;
    }

    private ParseParagraphAlgo paragraphAlgo;

    public WordEntityVO getWordEntityVO() {
        return wordEntityVO;
    }

    public ParseWord07BookMark setWordEntityVO(WordEntityVO wordEntityVO) {
        this.wordEntityVO = wordEntityVO;
        return this;
    }

    private WordEntityVO wordEntityVO;


    /**
     * 根据条件改变值
     *
     * @param map
     * @author JueYue
     * 2013-11-16
     */
    public void changeValues(XWPFParagraph paragraph, XWPFRun currentRun, String currentText,
                              List<Integer> runIndex, Map<String, Object> map,String color) throws Exception {
        System.out.println("----进入到changevalues中，进行替换数据，bookmark是:"+currentText+"\r\n");
        if(currentText.contains("p:("))
            System.out.println("处理图片");
        Object obj = PoiPublicUtil.getRealValue(currentText, map);
        // 如果是图片就设置为图片
        if (obj instanceof ImageEntity) {
            currentRun.setText("", 0);
            ExcelMapParse.addAnImage((ImageEntity) obj, currentRun);
            //steven add
            System.out.println("替换值为图片,图片的名称为："+((ImageEntity)obj).getUrl());
        } else {
            currentText = obj.toString();
            PoiPublicUtil.setWordText(currentRun, currentText);
            CTShd cTShd = currentRun.getCTR().addNewRPr().addNewShd();
            //String color="ffff00";
            if(color!=null)
            cTShd.setFill(color);
            System.out.println("替换值为文字,文字的内容为："+currentText);
        }
        for (int k = 0; k < runIndex.size(); k++) {
            paragraph.getRuns().get(runIndex.get(k)).setText("", 0);
        }
        runIndex.clear();
    }



    /**
     * 解析所有的文本
     *
     * @param paragraphs
     * @param map
     * @author JueYue
     * 2013-11-17
     */
    private void parseAllParagraphic(List<XWPFParagraph> paragraphs,
                                     Map<String, Object> map, Set<ParagraphEntityVO> paragraphEntities , String color, String paragraphSource) throws Exception {
        XWPFParagraph paragraph;

        int paracount=0;
        for (int i = 0; i < paragraphs.size(); i++) {
            paragraph = paragraphs.get(i);
            if (paragraph.getText().indexOf(START_STR) != -1) {
                //steven addd
                ParagraphEntityVO p=new ParagraphEntityVO();
                p.setParaSequence(paracount);
                p.setId(UUID.randomUUID().toString());
                p.setParaText(paragraph.getText());
                p.setBookMarkFromHFPC(paragraphSource);
                paragraphEntities.add(p);
                paracount++;
                //parseThisParagraph(paragraph, map,p,color);
                paragraphAlgo.parseThisParagraph(paragraph, map,p,color);
            }

        }

    }


    private void parseThisRow(List<XWPFTableCell> cells, Map<String, Object> map, RowEntityVO rowEntityVO, String color) throws Exception {
        int columnNo=0;
        for (XWPFTableCell cell : cells) {
            CellEntityVO cellEntityVO =new CellEntityVO();
            cellEntityVO.setId(UUID.randomUUID().toString());
            cellEntityVO.setCellHirzon(rowEntityVO.getRowIndex());
            cellEntityVO.setCellVertical(columnNo);
            cellEntityVO.setCellText(cell.getText());
            if(rowEntityVO.getCellEntityVOS()==null){
                Set<CellEntityVO> cellEntities=new HashSet<>();
                rowEntityVO.setCellEntityVOS(cellEntities);
            }
            rowEntityVO.getCellEntityVOS().add(cellEntityVO);
            System.out.println("行号是:+"+ rowEntityVO.getRowIndex()+"正在处理的单元格编号："+columnNo+" 该单元格中的内容是:"+cell.getText());
            List<XWPFParagraph> paragraphs=cell.getParagraphs();
            Set<ParagraphEntityVO> cellParagraphList=new HashSet<>();
            cellEntityVO.setParagraphEntityVOS(cellParagraphList);
            parseAllParagraphic(paragraphs,map,cellParagraphList,color,Constant.Cell);
//            parseAllParagraphic(paragraphs, map,cellEntityVO,color);
            columnNo++;
        }
    }


    /**
     * 解析这个表格
     *
     * @param table
     * @param map
     * @author JueYue
     * 2013-11-17
     */
    private void parseThisTable(XWPFTable table, Map<String, Object> map, TableEntityVO tableEntityVO, String color) throws Exception {
        XWPFTableRow row;
        List<XWPFTableCell> cells;
        Object listobj;

        if (tableEntityVO.getRowEntityVOS() == null) {
            Set<RowEntityVO> rowEntities = new HashSet<>();
            tableEntityVO.setRowEntityVOS(rowEntities);
        }
        int initialrowCount=table.getNumberOfRows();
        int beforeLooprowCount=table.getNumberOfRows();
        int afterLooprowCount=table.getNumberOfRows();
        int different=0;
        for (int i = 0; i < initialrowCount; i++) {
            //记住table上次增加的行数
            afterLooprowCount=table.getNumberOfRows();
            different=afterLooprowCount-beforeLooprowCount;
            beforeLooprowCount=afterLooprowCount;
            row = table.getRow(i+different);
            RowEntityVO rowEntityVO = new RowEntityVO();
            tableEntityVO.getRowEntityVOS().add(rowEntityVO);
            rowEntityVO.setId(UUID.randomUUID().toString());
            rowEntityVO.setRowIndex(i);
            rowEntityVO.setRowFirstColumnText(row.getCell(0).getText() != null ? row.getCell(0).getText().trim() : "");
            cells = row.getTableCells();
            String text = rowEntityVO.getRowFirstColumnText();//cells.get(0).getText()!=null?cells.get(0).getText().trim():"";
            // 判断是不是迭代输出
            if (text.length() == 0) continue;//本行不行就执行下一行；有rows但是没有cells
            //这个地方有错误，因为如何第一列为空，不可能是循环，但是可能是不循环的。
            boolean loopRow = text.contains(FOREACH);

            if (text != null && loopRow == false) {
                rowEntityVO.setLoopIndex(false);
            } else {
                rowEntityVO.setLoopIndex(true);
            }
            //正常行的处理
            if (!rowEntityVO.isLoopIndex()) {
                System.out.println("当前正在处理行,该行没有循环" + " 第" + rowEntityVO.getRowIndex() + "，行的第一个列字段值是:" + text + "\r\n");
                //if(cells.get(0).getText().contains("{{fe:}}{{username}}"))
                parseThisRow(cells, map, rowEntityVO,color);
            }
            //循环的行
            if (rowEntityVO.isLoopIndex()) {
                if (map.size() == 0) {
                    System.out.println("当前正在处理行,该行有循环" + " 第" + rowEntityVO.getRowIndex() + "，该行的第一个列字段值是:" + text + "\r\n");
                    String[] keys = text.replaceAll("\\s{1,}", " ").trim().split(" ");
                    if (rowEntityVO.getRowFirstColumnText().contains("{{type}}")) {
                        System.out.println("Debug fe type的的地方，" + "keys[0]" + keys[0] + "keys[1]" + keys[1] + "\r\n");
                    }
                    //这个地方，采用cells.get(0).SetText()是没有用的。无法改变这个cell中的值
                    cells.get(0).setText("");
                    System.out.println("after settext.it should be 空:" + cells.get(0).getText() + "**********************" + "\r\n");
                    cells.get(0).setText(keys[1]);
                    parseThisRow(cells, map, rowEntityVO,color);
                }
                if (map.size() > 0) {
                    //说明有赋值，准备进行解析
                    parseThisRowToValue(table,row,map, rowEntityVO,i,color);
                }
            }
        }
    }

    private void parseThisRowToValue(XWPFTable table, XWPFTableRow xwpfTableRow, Map<String,Object> map, RowEntityVO rowEntityVO, int i, String color) throws Exception{
        //先在Map中找到这个行对应的对象，这个对象是List。
        String tabletext=table.getText();
        String rowno=String.valueOf(rowEntityVO.getRowIndex());
        String key=tabletext.hashCode()+"#"+ rowno;
        List<Map<String,Object>> listobj= (List<Map<String,Object>>)map.get(key);
        if(listobj==null) listobj= (List<Map<String,Object>>)map.get("-777161730#1");
        parseNextRowAndAddRow(table, i, (List)listobj,color);
        i = i + ((List)listobj).size() - 1;
    }

    /**
     * 解析下一行,并且生成更多的行
     * @param table
     * @param index
     * @param list
     * Steven Copyfromorigun 2020-2-13
     */
    public static void parseNextRowAndAddRow(XWPFTable table, int index,
                                             List<TablePara> list,String color) throws Exception {
        XWPFTableRow currentRow = table.getRow(index);
        if(index==1){
            int test=0;
        }
        String[] params =ExcelMapParse.parseCurrentRowGetParams(currentRow);
        String listname = params[0];
        boolean isCreate = !listname.contains(FOREACH_NOT_CREATE);
        listname = listname.replace(FOREACH_NOT_CREATE, EMPTY).replace(FOREACH_AND_SHIFT, EMPTY)
                .replace(FOREACH, EMPTY).replace(START_STR, EMPTY);
        //String[] keys = listname.replaceAll("\\s{1,}", " ").trim().split(" ");
        //params[0] = keys[1];
        params[0]=listname.replace("{{fe:}}","");
        //steven add 2020-2-3
        for(String each:params){
            System.out.println("表格中循环的bookmark"+each.toString()+"\r\n");
        }
        //保存这一行的样式是-后面好统一设置
        List<XWPFTableCell> tempCellList = new ArrayList<XWPFTableCell>();
        tempCellList.addAll(table.getRow(index).getTableCells());
        int cellIndex = 0;
        Map<String, Object> tempMap = Maps.newHashMap();
        //LOGGER.debug("start for each dataofimagestring list :{}", list.size());
        String hightlightcolor="ffff00";
        String normallightcolor="ffffff";
        int listcount=0;
        if(list!=null) {

            for (Object obj : list) {
                currentRow = isCreate ? table.insertNewTableRow(index++) : table.getRow(index++);
                tempMap.put("t", obj);
                Map<String, Object> m = (Map<String, Object>) list.get(listcount);
                for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) {
                    //String val = eval(params[cellIndex], tempMap).toString();
                    //Map<String,Object> m= ListConvertToMap(list.get(listcount));
                    Object val = eval(params[cellIndex], m);
                    val=m.get(val);

                    for(String key :m.keySet()){
                        if(key.equals((String) val)){
                            val=m.get(key);
                            break;
                        }
                    }

                    currentRow.getTableCells().get(cellIndex).setText("");
                    copyCellAndSetValue(tempCellList.get(cellIndex),
                            currentRow.getTableCells().get(cellIndex), val, color);
                }

                for (; cellIndex < params.length; cellIndex++) {
                    //String val = eval(params[cellIndex], tempMap).toString();
                    //Map<String, Object> m = (Map<String, Object>) list.get(listcount);
                    //Map<String,Object> m= ListConvertToMap(list);
                    Object val = eval(params[cellIndex], m);
                    String valstring=((String)val).trim();
                    for(String key :m.keySet()){
                        if(key.trim().equals(valstring)){
                            val=m.get(key);
                            break;
                        }
                    }

                    copyCellAndSetValue(tempCellList.get(cellIndex),
                            currentRow.createCell(), val, color);

                }
                listcount++;
            }

        }
        //注意，这个地方index要累计才行，因为插入的时候，都插入到前面去了
        table.removeRow(index);
    }
    public static void copyCellAndSetValue(XWPFTableCell tmpCell, XWPFTableCell cell, Object obj,String color) throws Exception {
        CTTc cttc2 = tmpCell.getCTTc();
        CTTcPr ctPr2 = cttc2.getTcPr();

        CTTc cttc = cell.getCTTc();
        CTTcPr ctPr = cttc.addNewTcPr();
        if (tmpCell.getColor() != null) {
            cell.setColor(tmpCell.getColor());
        }
        if (tmpCell.getVerticalAlignment() != null) {
            cell.setVerticalAlignment(tmpCell.getVerticalAlignment());
        }
        if (ctPr2.getTcW() != null) {
            ctPr.addNewTcW().setW(ctPr2.getTcW().getW());
        }
        if (ctPr2.getVAlign() != null) {
            ctPr.addNewVAlign().setVal(ctPr2.getVAlign().getVal());
        }
        if (cttc2.getPList().size() > 0) {
            CTP ctp = cttc2.getPList().get(0);
            if (ctp.getPPr() != null) {
                if (ctp.getPPr().getJc() != null) {
                    cttc.getPList().get(0).addNewPPr().addNewJc().setVal(ctp.getPPr().getJc().getVal());
                }
            }
        }

        if (ctPr2.getTcBorders() != null) {
            ctPr.setTcBorders(ctPr2.getTcBorders());
        }

        XWPFParagraph tmpP = tmpCell.getParagraphs().get(0);
        XWPFParagraph cellP = cell.getParagraphs().get(0);
        XWPFRun tmpR = null;
        if (tmpP.getRuns() != null && tmpP.getRuns().size() > 0) {
            tmpR = tmpP.getRuns().get(0);
        }
        XWPFRun cellR = cellP.createRun();
        String currentText;
        //cellR.setText(text);

        // 如果是图片就设置为图片
        if (obj instanceof ImageEntity) {
            cellR.setText("", 0);
            ExcelMapParse.addAnImage((ImageEntity) obj, cellR);
            //steven add

        } else {
            currentText = obj.toString();
            PoiPublicUtil.setWordText(cellR, currentText);
        }


        //steven add
        CTShd cTShd = cellR.getCTR().addNewRPr().addNewShd();
        if(color!=null)
        cTShd.setFill(color);
        //cTShd.setFill("ffff00");//高亮显示，已经解决2020年2月4日,在表格中的修改的地方
        // cTShd.setFill("ffffff");//正常的显示
        //steven add finish

        //复制字体信息
        if (tmpR != null) {
            cellR.setBold(tmpR.isBold());
            cellR.setItalic(tmpR.isItalic());
            cellR.setStrike(tmpR.isStrike());
            cellR.setUnderline(tmpR.getUnderline());
            cellR.setColor(tmpR.getColor());
            cellR.setTextPosition(tmpR.getTextPosition());
            if (tmpR.getFontSize() != -1) {
                cellR.setFontSize(tmpR.getFontSize());
            }
            if (tmpR.getFontFamily() != null) {
                cellR.setFontFamily(tmpR.getFontFamily());
            }
            if (tmpR.getCTR() != null) {
                if (tmpR.getCTR().isSetRPr()) {
                    CTRPr tmpRPr = tmpR.getCTR().getRPr();
                    if (tmpRPr.isSetRFonts()) {
                        CTFonts tmpFonts = tmpRPr.getRFonts();
                        CTRPr cellRPr = cellR.getCTR().isSetRPr() ? cellR.getCTR().getRPr() : cellR.getCTR().addNewRPr();
                        CTFonts cellFonts = cellRPr.isSetRFonts() ? cellRPr.getRFonts() : cellRPr.addNewRFonts();
                        cellFonts.setAscii(tmpFonts.getAscii());
                        cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme());
                        cellFonts.setCs(tmpFonts.getCs());
                        cellFonts.setCstheme(tmpFonts.getCstheme());
                        cellFonts.setEastAsia(tmpFonts.getEastAsia());
                        cellFonts.setEastAsiaTheme(tmpFonts.getEastAsiaTheme());
                        cellFonts.setHAnsi(tmpFonts.getHAnsi());
                        cellFonts.setHAnsiTheme(tmpFonts.getHAnsiTheme());
                    }
                }
            }
        }
        //复制段落信息
        if (tmpP.getAlignment() != null) {
            cellP.setAlignment(tmpP.getAlignment());
        }
        if (tmpP.getVerticalAlignment() != null) {
            cellP.setVerticalAlignment(tmpP.getVerticalAlignment());
        }
        if (tmpP.getBorderBetween() != null) {
            cellP.setBorderBetween(tmpP.getBorderBetween());
        }
        if (tmpP.getBorderBottom() != null){
            cellP.setBorderBottom(tmpP.getBorderBottom());
        }
        if (tmpP.getBorderLeft() != null){
            cellP.setBorderLeft(tmpP.getBorderLeft());
        }
        if (tmpP.getBorderRight() != null){
            cellP.setBorderRight(tmpP.getBorderRight());
        }
        if (tmpP.getBorderTop() != null){
            cellP.setBorderTop(tmpP.getBorderTop());
        }
        cellP.setPageBreak(tmpP.isPageBreak());
        if (tmpP.getCTP() != null) {
            if (tmpP.getCTP().getPPr() != null) {
                CTPPr tmpPPr = tmpP.getCTP().getPPr();
                CTPPr cellPPr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();
                //复制段落间距信息
                CTSpacing tmpSpacing = tmpPPr.getSpacing();
                if (tmpSpacing != null) {
                    CTSpacing cellSpacing = cellPPr.getSpacing() != null ? cellPPr.getSpacing() : cellPPr.addNewSpacing();
                    if (tmpSpacing.getAfter() != null) {
                        cellSpacing.setAfter(tmpSpacing.getAfter());
                    }
                    if (tmpSpacing.getAfterAutospacing() != null) {
                        cellSpacing.setAfterAutospacing(tmpSpacing.getAfterAutospacing());
                    }
                    if (tmpSpacing.getAfterLines() != null) {
                        cellSpacing.setAfterLines(tmpSpacing.getAfterLines());
                    }
                    if (tmpSpacing.getBefore() != null) {
                        cellSpacing.setBefore(tmpSpacing.getBefore());
                    }
                    if (tmpSpacing.getBeforeAutospacing() != null) {
                        cellSpacing.setBeforeAutospacing(tmpSpacing.getBeforeAutospacing());
                    }
                    if (tmpSpacing.getBeforeLines() != null) {
                        cellSpacing.setBeforeLines(tmpSpacing.getBeforeLines());
                    }
                    if (tmpSpacing.getLine() != null) {
                        cellSpacing.setLine(tmpSpacing.getLine());
                    }
                    if (tmpSpacing.getLineRule() != null) {
                        cellSpacing.setLineRule(tmpSpacing.getLineRule());
                    }
                }
                //复制段落缩进信息
                CTInd tmpInd = tmpPPr.getInd();
                if (tmpInd != null) {
                    CTInd cellInd = cellPPr.getInd() != null ? cellPPr.getInd() : cellPPr.addNewInd();
                    if (tmpInd.getFirstLine() != null) {
                        cellInd.setFirstLine(tmpInd.getFirstLine());
                    }
                    if (tmpInd.getFirstLineChars() != null) {
                        cellInd.setFirstLineChars(tmpInd.getFirstLineChars());
                    }
                    if (tmpInd.getHanging() != null) {
                        cellInd.setHanging(tmpInd.getHanging());
                    }
                    if (tmpInd.getHangingChars() != null) {
                        cellInd.setHangingChars(tmpInd.getHangingChars());
                    }
                    if (tmpInd.getLeft() != null) {
                        cellInd.setLeft(tmpInd.getLeft());
                    }
                    if (tmpInd.getLeftChars() != null) {
                        cellInd.setLeftChars(tmpInd.getLeftChars());
                    }
                    if (tmpInd.getRight() != null) {
                        cellInd.setRight(tmpInd.getRight());
                    }
                    if (tmpInd.getRightChars() != null) {
                        cellInd.setRightChars(tmpInd.getRightChars());
                    }
                }
            }
        }
    }

    /**
     * 根据条件改变值
     *
     * @param map
     * @author Steven
     * 2020-2-13
     */
    private static Object eval(String currentText,Map<String, Object> map) throws Exception {
        System.out.println("----当前的para下的bookmark是:"+currentText+"\r\n");
        Object obj = PoiPublicUtil.getRealValue(currentText, map);//参考了changeValue
        return  obj;
    }





    /**
     * 解析07版的Word并且进行赋值
     *
     * @return
     * @throws Exception
     * @author JueYue
     * 2013-11-16
     */
    public XWPFDocument parseWord(String url, Map<String, Object> map,String color) throws Exception {
        MyXWPFDocument doc = WordCache.getXWPFDocument(url);
        parseWordSetValue(doc, map,color);
        return doc;
    }

    /**
     * 2020年2月20日，补充这个文件InputStream传递的方法
     * @param is
     * @param map
     * @param color
     * @return
     * @throws Exception
     */
    public XWPFDocument parseWord(InputStream is, Map<String, Object> map, String color) throws Exception {
        MyXWPFDocument doc = WordCache.getXWPFDocument(is);
        parseWordSetValue(doc, map,color);
        return doc;
    }

    /**
     * 解析07版的Work并且进行赋值但是进行多页拼接
     *
     * @param url
     * @param list
     * @return
     */
    public XWPFDocument parseWord(String url, List<Map<String, Object>> list,String color) throws Exception {
        if (list == null || list.size() == 0) {
            return null;
        } else if (list.size() == 1) {
            return parseWord(url, list.get(0),color);
        } else {
            MyXWPFDocument doc = WordCache.getXWPFDocument(url);
            parseWordSetValue(doc, list.get(0),color);
            //插入分页
            doc.createParagraph().setPageBreak(true);
            for (int i = 1; i < list.size(); i++) {
                MyXWPFDocument tempDoc = WordCache.getXWPFDocument(url);
                parseWordSetValue(tempDoc, list.get(i),color);
                tempDoc.createParagraph().setPageBreak(true);
                doc.getDocument().addNewBody().set(tempDoc.getDocument().getBody());

            }
            return doc;
        }

    }



    private void parseWordSetValue(MyXWPFDocument doc, Map<String, Object> map,String color) throws Exception {
        //pringWordBody(doc);
        System.out.println("开始解析文档"+doc.toString()+"：------------------------------------------------------- ");
        // 第一步解析页眉,页脚
        System.out.println("开始处理页眉及页脚：---------------------------------------------------- ");
        parseHeaderAndFoot(doc, map,color);
        // 第二步解析文档
        System.out.println("开始处理正常的段落：------------------------------------------------------- ");
        List<XWPFParagraph> paragraphs= doc.getParagraphs();

        Set<ParagraphEntityVO> wordparagraphEntities= wordEntityVO.getParagraphEntityVOS();
        parseAllParagraphic(paragraphs, map,wordparagraphEntities,color,Constant.Paragraph);

        // 第三步解析所有表格
        System.out.println("开始处理表格：----------------------------------------------------------- ");
        XWPFTable           table;
        Iterator<XWPFTable> itTable = doc.getTablesIterator();
        int tableno=0;//steven增加
        while (itTable.hasNext()) {
            table = itTable.next();
            System.out.println("Debug当前处理的表格内容是:"+table.getText()+"\r\n");
            if (table.getText().indexOf(START_STR) != -1) {
                tableno++;
                String tableText=table.getText();
                //保证table的编号不重复。因为有循环的变量
                System.out.println("目前处理的表格是当前文档中的顺序号："+tableno+"\r\n");
                System.out.println("tableText.hashCode()"+tableText.hashCode()+"_"+tableno+"\r\n");
                System.out.println("当前处理的表格内容是:"+table.getText()+"\r\n");
                TableEntityVO tableEntityVO =new TableEntityVO();
                tableEntityVO.setId(UUID.randomUUID().toString());
                tableEntityVO.setTableText(tableText);
                tableEntityVO.setTableSequence(tableno);
                tableEntityVO.setTableHashcode(String.valueOf(tableText.hashCode()));

                System.out.println(tableText+"-tableText.hashCode()"+":"+tableText.hashCode());
                wordEntityVO.getTableEntityVOS().add(tableEntityVO);
                parseThisTable(table, map, tableEntityVO,color);
            }
        }

    }

    /**
     * 解析页眉和页脚
     *
     * @param doc
     * @param map
     * @throws Exception
     */
    private void parseHeaderAndFoot(MyXWPFDocument doc, Map<String, Object> map,String color) throws Exception {
        List<XWPFHeader> headerList = doc.getHeaderList();
        for (XWPFHeader xwpfHeader : headerList) {
            HeaderEntityVO headerEntityVO =new HeaderEntityVO();
            headerEntityVO.setId(UUID.randomUUID().toString());
            headerEntityVO.setHeaderText(xwpfHeader.getText());
            wordEntityVO.getHeaderEntityVOS().add(headerEntityVO);
            Set<ParagraphEntityVO> headerEntityParagraphset=new HashSet<>();
            headerEntityVO.setParagraphEntityVOS(headerEntityParagraphset);
            parseAllParagraphic(xwpfHeader.getListParagraph(),map,headerEntityParagraphset,color,Constant.Header);


        }
        List<XWPFFooter> footerList = doc.getFooterList();
        for (XWPFFooter xwpfFooter : footerList) {
            FootEntityVO footEntityVO =new FootEntityVO();
            footEntityVO.setId(UUID.randomUUID().toString());
            footEntityVO.setFooterText(xwpfFooter.getText());
            wordEntityVO.getFootEntityVOS().add(footEntityVO);
            Set<ParagraphEntityVO> footEntityParagraphset=new HashSet<>();
            footEntityVO.setParagraphEntityVOS(footEntityParagraphset);
            parseAllParagraphic(xwpfFooter.getListParagraph(),map,footEntityParagraphset,color,Constant.Foot);

        }

    }


}
